<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          style="width: 240px; height: 65px; resize: none"
          src="../assets/title.png"
        />
        <span>文件管理系统</span>
      </div>
      <!-- 头像 -->
      <div>
        <el-avatar
          style="margin-right: 20px"
          :size="50"
          shape="square"
          :src="userInfo.avatar"
        ></el-avatar>
        <el-dropdown>
          <p class="el-dropdown-link">
            <!-- 用户名 -->
            欢迎 {{ userInfo.username
            }}<i class="el-icon-arrow-down el-icon--right"></i>
          </p>
          <!-- 下拉菜单 -->
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-plus"> 新建用户</el-dropdown-item>
            <el-dropdown-item icon="el-icon-refresh">刷新页面</el-dropdown-item>
            <el-dropdown-item icon="el-icon-house"> 回到主页</el-dropdown-item>
            <el-dropdown-item icon="el-icon-edit"> 修改信息</el-dropdown-item>
            <el-dropdown-item
              icon="el-icon-switch-button"
              @click.native="logout"
              >退出登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '210px'">
        <div class="toggle-button" @click="changeMenuCollapse">|||</div>
        <el-menu
          router
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
        >
          <!-- 一级菜单 -->
          <el-menu-item
            :index="'/'+item.path"
            v-for="item in menuList"
            :key="item.id"
            @click="saveNavState('/'+item.path)"
          >
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </template>
          </el-menu-item>
        </el-menu>
        <!-- 侧边栏结束 -->
      </el-aside>
      <el-container>
        <el-main>
          <!-- 右侧主体 -->
          <router-view></router-view>
        </el-main>
        <!-- 右侧footer -->
        <el-footer style="height:30px" class="footer-text">@Marktotop-MarkWang  2021-04</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 用户信息
      userInfo: {},
      // 菜单列表
      menuList: [
        { id: 101, 
          menuName: '人员管理', 
          path: 'user', 
          icon: 'el-icon-user' 
        },
        {
          id: 102,
          menuName: '文件管理',
          path: 'file',
          icon: 'el-icon-folder-opened',
        },
        {
          id: 103,
          menuName: '接口管理',
          path: 'api',
          icon: 'el-icon-notebook-2',
        },
        {
          id: 104,
          menuName: '项目监控',
          path: 'druid',
          icon: 'el-icon-data-analysis',
        }
      ],
      isCollapse: false,
      // 被激活的菜单路径
      activePath:''
    }
  },
  created() {
    this.getProfile()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 改变侧边栏状态，是否折叠
    changeMenuCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存点击的侧边栏路径
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    },
    // 退出功能 ,清除session存储中的secretKey
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('您已成功退出系统！')
    },
    // 获取用户信息 ,通过存储中的用户名，secretKey
    async getProfile() {
      const username = window.sessionStorage.getItem('secretKey')
      const { data: res } = await this.$http.get('profile', {
        //params参数必写 , 如果没有参数传{}也可以
        params: {
          username: username,
        },
      })
      if (res.code !== 20000) return this.$message.error(res.message)
      this.userInfo = res.data.user
    },
  },
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #3c383a;
  padding-left: 20px;
  padding-right: 20px;
  align-items: center;
  color: rgb(184, 176, 176);
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}

.el-dropdown-link {
  font-size: 16px;
  color: #adaeb1;
  cursor: pointer;
  display: flex;
  align-items: center;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.toggle-button {
  font-size: 14px;
  line-height: 26px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  background-color: rgb(117, 103, 103);
}
.footer-text{
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: rgb(167, 157, 157);
}
</style>